<?php
namespace Admin\Controller;

use Think\Controller;
use Admin\Model\HospitalModel;

class HospitalController extends Controller {

	protected $instance = NULL;
	
	public function __construct()
	{
		parent::__construct();
		if($this->instance == NULL) {
			$this->instance = new HospitalModel;
		}		
	}
	
	public function index()
	{
		$count      = $this->instance->count();	
		$Page       = new \Think\Page($count,10);
		$Page->setConfig('theme',"<ul class='pagination'></li><li>%FIRST%</li><li>%UP_PAGE%</li><li>%LINK_PAGE%</li><li>%DOWN_PAGE%</li><li>%END%</li><li><span> %HEADER%  %NOW_PAGE%/%TOTAL_PAGE% 页 </span></li></ul>");
		$Pageshow       = $Page->show();		
		$index = [
					"limit"	=> array(
							"firstRow" => $Page->firstRow,
							"listRows" => $Page->listRows,
						),
					];
		$hospital = $this->instance->index($index);
		$this->assign('hospital',$hospital);
		$this->assign('Pageshow',$Pageshow);
		$this->display();
	}
	
	// 显示添加医院界面
	public function addHospitalPage()
	{
		return $this->display();
	}
	
	// 添加医院
	public function addHospital()
	{
		$data["name"] = I("post.name");	
		$status = $this->instance->addHospital($data);
		if($status) {
			return $this->success('添加成功', U('Admin/Hospital/index'));
		}			
		else{
			return $this->success('添加失败', U('Admin/Hospital/addHospitalPage'));
		}
	}
	
	public function editHospitalPage()
	{
		$data['find'] = I('get.id');
		$hospital = $this->instance->showHospital($data);
		$this->assign('hospital',$hospital);
		return $this->display();
	}
	

	public function editHospital()
	{
		$data["name"]  = I("post.name");
		$data["id"]    = I("post.id");
		$status = $this->instance->editHospital($data);
		if($status) {
			return $this->success('修改成功', U('Admin/Hospital/index'));
		}			
		else{
			return $this->success('修改失败', U('Admin/Hospital/index'));
		}
	}
	
	public function stopHospital()
	{
		$data['id']= I("get.id");
		$data['status'] = 0;
		$status = $this->instance->stopHospital($data);
		if($status) {
			return $this->success('停用成功', U('Admin/Hospital/index'));
		}			
		else{
			return $this->success('停用失败', U('Admin/Hospital/index'));
		}
	}
	
	public function powerHospital()
	{
		$data['id']= I("get.id");
		$data['status'] = 1;
		$status = $this->instance->stopHospital($data);
		if($status) {
			return $this->success('停用成功', U('Admin/Hospital/index'));
		}			
		else{
			return $this->success('停用失败', U('Admin/Hospital/index'));
		}
	}
	
}